﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NPOI.OpenXmlFormats.Shared;
using S4Store.Domain;
using S4Store.Service;

namespace S4Store.Controllers
{
    public class HomeController : BaseController
    {
        public ImproveGuideService ImproveGuideService { get; set; }
        public ActionplanService ActionplanService { get; set; }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Welcome()
        {
            return View();
        }
        public String Logout()
        {
            using (var t = new Transtion())
            {
                t.Inject(AdministratorService);
                AdministratorService.Logout();
                t.Commit();
                
            }
            return "<script>alert('已安全退出');location.href='/Login'</script>";
        }

        public JsonResult GetMessageResult()
        {
            using (var t = new Transtion())
            {
                t.Inject(ImproveGuideService);
                var type = 0;
                switch (CurrentUser.Position)
                {
                    case Position.SalesManager:
                        type = ImproveGuide.ImproveGuideType.Sale.ToInt();
                        break;
                    case Position.MarketingManager:
                        type = ImproveGuide.ImproveGuideType.Markting.ToInt();
                        break;
                    case Position.AftersalesManager:
                        type = ImproveGuide.ImproveGuideType.AfterSale.ToInt();
                        break;
                }
                IQueryable<Domain.ImproveGuide> query = null;
                if (CurrentUser.Position == Position.SalesManager
                    || CurrentUser.Position == Position.MarketingManager
                    || CurrentUser.Position == Position.AftersalesManager)
                {
                    query = ImproveGuideService.GetQuery()
                        .Where(x => x.Type == type && x.StoreId == CurrentUser.StoreId.Value &&
                                x.StatusValue == (int) S4Store.Domain.ImproveGuide.Status.Official && x.IsRead == false);
                }
                if (CurrentUser.Position == Position.StoreManager || CurrentUser.Position == Position.SystemManager)
                {
                    query = ImproveGuideService.GetQuery()
                        .Where(x=> x.StoreId == CurrentUser.StoreId.Value &&
                                x.StatusValue == (int)S4Store.Domain.ImproveGuide.Status.Official && x.IsManagerRead == false);
                }
                if (query != null && query.Any())
                {
                    return new JsonResult
                    {
                        Data = new { success = true, num=query.Count()>10?9:query.Count() }
                    };
                }
                return new JsonResult
                {
                    Data = new { success = false }
                };
            }
        }

        public JsonResult GetActionResult()
        {
            using (var t = new Transtion())
            {
                t.Inject(ActionplanService);
                if (CurrentUser.Position != Position.StoreManager)
                {
                    return new JsonResult
                    {
                        Data = new { success = false }
                    };
                }

                var query = ActionplanService.GetQuery()
                        .Where(x => x.StoreId == CurrentUser.StoreId.Value && x.PlanStatusValue!=4);
                if (query != null && query.Any())
                {
                    return new JsonResult
                    {
                        Data = new { success = true, num = query.Count() > 10 ? 9 : query.Count() }
                    };
                }
                return new JsonResult
                {
                    Data = new { success = false }
                };
            }
        }
    }
}
